package org.monster.uiboot;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import org.monster.boot.ApplicationMediator;
import org.monster.boot.ServiceMediator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * UI启动器
 */
public class UIStartover {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                CommandFrame frame = new CommandFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

/**
 * 命令片段
 */
class CommandFrame extends JFrame {

    /**
     * 服务中介者
     */
    private static final ServiceMediator mediator = new ServiceMediator();
    /**
     * Dos服务中介者
     */
    private static final ServiceMediator dosMediator = new ServiceMediator();
    /**
     * 应用服务中介者
     */
    private static final ApplicationMediator applicationMediator = new ApplicationMediator();
    /**
     * 面板
     */
    private static final JPanel panel = new JPanel();
    /**
     * 控制台问候语
     */
    private static final JTextArea console = new JTextArea("Welcome to Monster Server!");

    /**
     * 静态代码块
     */
    static {
        mediator.introduce();
        dosMediator.introduce();
        applicationMediator.introduce();
        try {
            UIManager.setLookAndFeel(WindowsLookAndFeel.class.getName());
            SwingUtilities.updateComponentTreeUI(panel);
        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
    }

    /**
     * 构造方法
     */
    public CommandFrame() {
        setTitle("Monster");
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(dimension.width / 2, dimension.height / 2);
        setLocation(dimension.width / 4, dimension.height / 4);
        console.setRows(15);
        console.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(console);
        add(scrollPane, BorderLayout.NORTH);

        addJButton("添加项目", new AddAction(CommandFrame.this));
        addJButton("删除项目", new RemoveAction(CommandFrame.this));
        addJButton("编译项目", new CompileAction(CommandFrame.this));
        addJButton("重置服务", new ResetAction());

        add(panel);

        JTextField textField = new JTextField();
        Dimension textFieldSize = new Dimension(dimension.width / 2 - 200, 25);
        textField.setPreferredSize(textFieldSize);
        JLabel jLabel = new JLabel("Command:");
        jLabel.setFont(new Font("Consoles", Font.PLAIN, 13));
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new FlowLayout(SwingConstants.CENTER));
        JButton jButton = new JButton("提交Command");
        jButton.addActionListener(new CommandAction(textField));
        jPanel.add(jLabel);
        jPanel.add(textField);
        jPanel.add(jButton);
        add(jPanel, BorderLayout.SOUTH);

        UILog.setArea(console);
    }

    /**
     * 添加按钮
     *
     * @param title    标题
     * @param listener 活动监听器
     */
    public void addJButton(String title, ActionListener listener) {
        JButton jButton = new JButton(title);
        jButton.addActionListener(listener);
        panel.add(jButton);
    }
}
